package org.jeecg.modules.util;

import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.modules.driver.DriverPortocal1_0;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.rmi.runtime.Log;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DeviceMsgHelper_PD1_0 {
    static DecimalFormat df = new DecimalFormat("#.00");
    public static JSONObject anaylysBaseInfo(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        int snLen = byteData[0];
        byte[] sn = new byte[snLen];
        byte[] mac = new byte[12];
        byte model = byteData[snLen+13];
        byte tyoe =  byteData[snLen+14];
        System.arraycopy(byteData,1,sn,0,snLen);
        System.arraycopy(byteData,1+snLen,mac,0,12);
        String snStr = ByteUtils.toHexStringDisplay(sn).replace(" ","");
        String macStr = ByteUtils.toHexStringDisplay(mac).replace(" ",":");
        shadow.getJSONObject("baseinfo").put("SN",snStr);
        shadow.getJSONObject("network").put("mac",macStr);
        shadow.getJSONObject("baseinfo").put("model",(int)model + "");
        shadow.getJSONObject("baseinfo").put("tyoe",(int)tyoe + "");
        return shadow;
    }

    public static JSONObject anaylysDeviceSetConfig(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);

        //log.info("anaylysDeviceSetConfig start");
        JSONObject baseinfo = shadow.getJSONObject("baseinfo");
        baseinfo.put("mainVolMax",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))/10.0F));
        baseinfo.put("mainVolMin",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))/10.0F));
        baseinfo.put("mainCurMax",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))/10.0F));
        baseinfo.put("backVolMax",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0))/10.0F));
        baseinfo.put("backVolMin",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,8,9),0))/10.0F));
        baseinfo.put("tempMax",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,10,11),0))/10.0F));
        baseinfo.put("fanTempMax",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,12,13),0))/10.0F));
        shadow.put("baseinfo",baseinfo);
        //log.info("anaylysDeviceSetConfig baseinfo" + baseinfo.toString());
        return shadow;
    }

    public static JSONObject anaylysDeviceDhcpIpPort(JSONObject shadow, String msg) throws Exception  {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        String ip = ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))+"."+
                        ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))+"."+
                                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0));
        int port = ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,8,9),0));
        shadow.getJSONObject("network").put("ip",ip);
        shadow.getJSONObject("network").put("port",port+"");
        return shadow;
    }

    public static JSONObject anaylysDeviceSubnetMask(JSONObject shadow, String msg) throws Exception  {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        String mask = ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0));
        shadow.getJSONObject("network").put("subnet",mask);
        return shadow;
    }

    public static JSONObject anaylysDeviceGateway(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        String gateway = ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0));
        shadow.getJSONObject("network").put("gateway",gateway);
        return shadow;
    }

    public static JSONObject anaylysDeviceMac(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        byte[] byteDataMac = new byte[6];
        for(int i=0;i<6;i++){
            byteDataMac[i] = byteData[i*2+1];
        }
        String macStr = ByteUtils.toHexStringDisplay(byteDataMac).replace(" ",":");
        macStr = macStr.substring(0,macStr.length()-1);
        shadow.getJSONObject("network").put("mac",macStr);
        return shadow;
    }


    public static JSONObject anaylysDeviceDNS(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        String dns = ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))+"."+
                ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0));
        shadow.getJSONObject("network").put("dns",dns);
        return shadow;
    }

    public static JSONObject anaylysDeviceCurrent(JSONObject shadow, String msg)  throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject switches = shadow.getJSONObject("switches");
        JSONArray swa = switches.getJSONArray("switches");
        for(int i=0;i<switches.getInt("total");i++){
            swa.getJSONObject(i).put("current", df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,i*2,i*2+1),0))/10.0F));
        }
        switches.put("switches",swa);
        shadow.put("switches",switches);
        return shadow;
    }

    public static JSONObject anaylysDeviceSwitch(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject switches = shadow.getJSONObject("switches");
        JSONArray swa = switches.getJSONArray("switches");
        for(int i=0;i<switches.getInt("total");i++){
            swa.getJSONObject(i).put("state", ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,i*2,i*2+1),0)));
        }
        switches.put("switches",swa);
        shadow.put("switches",switches);
        return shadow;
    }

    public static JSONObject anaylysDeviceCurrentLimit(JSONObject shadow, String msg) throws Exception{
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject switches = shadow.getJSONObject("switches");
        JSONArray swa = switches.getJSONArray("switches");
        for(int i=0;i<switches.getInt("total");i++){
            swa.getJSONObject(i).put("controlC", df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,i*2,i*2+1),0))/10.0F));
        }
        switches.put("switches",swa);
        shadow.put("switches",switches);
        return shadow;
    }

    public static JSONObject anaylysDeviceWarningDevice(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject warning = shadow.getJSONObject("warning");
        warning.put("cityVol",ByteUtils.getShort(new byte[]{byteData[1],byteData[0]},0));
        warning.put("mainVol",ByteUtils.getShort(new byte[]{byteData[3],byteData[2]},0));
        warning.put("current",ByteUtils.getShort(new byte[]{byteData[5],byteData[4]},0));
        warning.put("ups",ByteUtils.getShort(new byte[]{byteData[7],byteData[6]},0));
        warning.put("backVol",ByteUtils.getShort(new byte[]{byteData[9],byteData[8]},0));
        warning.put("temp",ByteUtils.getShort(new byte[]{byteData[11],byteData[10]},0));
        shadow.put("warning",warning);
        return shadow;
    }

    public static JSONObject anaylysDeviceWanringChannel(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject switches = shadow.getJSONObject("switches");
        JSONArray swa = switches.getJSONArray("switches");
        for(int i=0;i<switches.getInt("total");i++){
            swa.getJSONObject(i).put("iswarning", ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,i*2,i*2+1),0)));
        }
        switches.put("switches",swa);
        shadow.put("switches",switches);
        return shadow;
    }

    public static JSONObject anaylysDeviceMainStatus(JSONObject shadow, String msg) throws Exception {
        byte[] byteData = msg.getBytes(DriverPortocal1_0.ENCODING);
        JSONObject baseinfo = shadow.getJSONObject("baseinfo");
        baseinfo.put("mainVol",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,0,1),0))/10.0F));
        baseinfo.put("backVol",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,2,3),0))/10.0F));
        baseinfo.put("supplyCurrent",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,4,5),0))/10.0F));
        baseinfo.put("input",ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,6,7),0)));
        baseinfo.put("output",ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,8,9),0)));
        baseinfo.put("controllMode",ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,10,11),0)));;
        baseinfo.put("isWarning",ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,12,13),0)));;
        baseinfo.put("temp",df.format(ByteUtils.getUnsignedShort(ByteUtils.getShort(ByteUtils.switchTwoByte(byteData,14,15),0))/10.0F));
        shadow.put("baseinfo",baseinfo);
        return shadow;
    }

    public static JSONObject anaylysDLocalDisconnect(JSONObject shadow, String msg) throws Exception {
        shadow.getJSONObject("baseinfo").put("isOnline","0");
        shadow.put("offTime",new Date().getTime());
        return shadow;
    }

    public static JSONObject deviceConnected(JSONObject shadow, String msg) throws Exception {
        shadow.getJSONObject("baseinfo").put("isOnline",1);
        shadow.put("onTime", Calendar.getInstance().getTime().getTime());
        return shadow;
    }
}
